import java.util.stream.Collectors;

public class 找出字符串中第一个匹配项的下标 {

    public static void main(String[] args) {
        System.out.println("leetcode".indexOf("code"));
        System.out.println(strStr("leetcode", "code"));
    }

    public static int strStr(String haystack, String needle) {
        int index = -1;
        for (int i = 0; i < haystack.length(); i++) {
            int j;
            for (j = 0; j < needle.length() && i + j < haystack.length(); j++) {
                char a = haystack.charAt(i + j);
                char b = needle.charAt(j);
                if (a != b) {
                    break;
                }
            }
            if (j == needle.length()) {
                index = i;
                break;
            }
        }
        return index;
    }
}
